home *** CD-ROM | disk | FTP | other *** search
/ Dr. Windows 3 / dr win3.zip / dr win3 / PROGRAMR / OLE2BOOK.ZIP / CHAP08.ZIP / CHAP08 / PATRON / PAGEWIN.CPP < prev    next >
C/C++ Source or Header  |  1993-06-17  |  23KB  |  832 lines

  1. /*
  2.  * PAGEWIN.CPP
  3.  * Modifications for Chapter 8
  4.  *
  5.  * Window procedure for the Pages window and support functions.  This
  6.  * window manages its own scrollbars and viewport and provides
  7.  * printing capabilities as well.  The public CPages::Print lives here.
  8.  *
  9.  * Copyright (c)1993 Microsoft Corporation, All Rights Reserved
  10.  *
  11.  * Kraig Brockschmidt, Software Design Engineer
  12.  * Microsoft Systems Developer Relations
  13.  *
  14.  * Internet  :  kraigb@microsoft.com
  15.  * Compuserve:  >INTERNET:kraigb@microsoft.com
  16.  */
  17.  
  18.  
  19.  
  20. #include "patron.h"
  21.  
  22.  
  23. extern HWND g_hDlgPrint;
  24. extern BOOL g_fCancelPrint;
  25.  
  26.  
  27. /*
  28.  * PagesWndProc
  29.  *
  30.  * Purpose:
  31.  *  Window procedure for the Pages window.
  32.  */
  33.  
  34. LRESULT __export FAR PASCAL PagesWndProc(HWND hWnd, UINT iMsg
  35.     , WPARAM wParam, LPARAM lParam)
  36.     {
  37.     LPCPages        ppg;
  38.     PAINTSTRUCT     ps;
  39.     HDC             hDC;
  40.     int             iPos, iTmp;
  41.     int             iMin, iMax;
  42.     UINT            idScroll;
  43.     BOOL            fDirty=FALSE;
  44.  
  45.     ppg=(LPCPages)GetWindowLong(hWnd, PAGEWL_STRUCTURE);
  46.  
  47.     switch (iMsg)
  48.         {
  49.         case WM_CREATE:
  50.             ppg=(LPCPages)((LPCREATESTRUCT)lParam)->lpCreateParams;
  51.             SetWindowLong(hWnd, PAGEWL_STRUCTURE, (LONG)ppg);
  52.  
  53.             ppg->m_hWnd=hWnd;
  54.             break;
  55.  
  56.         case WM_PAINT:
  57.             //CHAPTER8MOD
  58.             /*
  59.              * If there is currently a drag-rectangle showing, then
  60.              * remove it before painting.  This insures that painting
  61.              * doesn't blast part of that rectangle away such that when
  62.              * we draw it next, garbage is left around.
  63.              */
  64.             if (ppg->m_fDragRectShown)
  65.                 ppg->DrawDropTargetRect(NULL, NULL);
  66.             //End CHAPTER8MOD
  67.  
  68.             hDC=BeginPaint(hWnd, &ps);
  69.  
  70.             //Draw only if we have a page to show.
  71.             if (0!=ppg->m_cPages)
  72.                 ppg->Draw(hDC, FALSE, FALSE);
  73.  
  74.             EndPaint(hWnd, &ps);
  75.  
  76.             //CHAPTER8MOD
  77.             //Turn the rectangle back on, if necessary.
  78.             if (ppg->m_fDragRectShown)
  79.                 ppg->DrawDropTargetRect(NULL, NULL);
  80.             //End CHAPTER8MOD
  81.             break;
  82.  
  83.  
  84.         case WM_HSCROLL:
  85.         case WM_VSCROLL:
  86.             idScroll=(WM_HSCROLL==iMsg) ? SB_HORZ : SB_VERT;
  87.  
  88.             iPos=GetScrollPos(hWnd, idScroll);
  89.             iTmp=iPos;
  90.             GetScrollRange(hWnd, idScroll, &iMin, &iMax);
  91.  
  92.             switch (wParam)
  93.                 {
  94.                 case SB_LINEUP:     iPos -= 20;  break;
  95.                 case SB_PAGEUP:     iPos -=100;  break;
  96.                 case SB_LINEDOWN:   iPos += 20;  break;
  97.                 case SB_PAGEDOWN:   iPos +=100;  break;
  98.  
  99.                 case SB_THUMBPOSITION:
  100.                     iPos=ScrollThumbPosition(wParam, lParam);
  101.                     break;
  102.  
  103.                 //We don't want scrolling on this message.
  104.                 case SB_THUMBTRACK:
  105.                     return 0L;
  106.                 }
  107.  
  108.             iPos=max(iMin, min(iPos, iMax));
  109.  
  110.             if (iPos!=iTmp)
  111.                 {
  112.                 //Set the new position and scroll the window as necessary.
  113.                 SetScrollPos(hWnd, idScroll, iPos, TRUE);
  114.  
  115.                 if (SB_HORZ==idScroll)
  116.                     {
  117.                     ppg->m_xPos=iPos;
  118.                     ScrollWindow(hWnd, iTmp-iPos, 0, NULL, NULL);
  119.                     }
  120.                 else
  121.                     {
  122.                     ppg->m_yPos=iPos;
  123.                     ScrollWindow(hWnd, 0, iTmp-iPos, NULL, NULL);
  124.                     }
  125.                 }
  126.  
  127.             break;
  128.  
  129.         case WM_LBUTTONDOWN:
  130.             if (NULL==ppg->m_pPageCur)
  131.                 break;
  132.  
  133.             fDirty=ppg->m_pPageCur->OnLeftDown(wParam
  134.                 , LOWORD(lParam), HIWORD(lParam));
  135.             break;
  136.  
  137.         case WM_LBUTTONUP:
  138.             if (NULL==ppg->m_pPageCur)
  139.                 break;
  140.  
  141.             fDirty=ppg->m_pPageCur->OnLeftUp(wParam
  142.                 , LOWORD(lParam), HIWORD(lParam));
  143.             break;
  144.  
  145.         case WM_LBUTTONDBLCLK:
  146.             if (NULL==ppg->m_pPageCur)
  147.                 break;
  148.  
  149.             fDirty=ppg->m_pPageCur->OnLeftDoubleClick(wParam, LOWORD(lParam)
  150.                 , HIWORD(lParam));
  151.             break;
  152.  
  153.         case WM_MOUSEMOVE:
  154.             if (NULL==ppg->m_pPageCur)
  155.                 break;
  156.  
  157.             ppg->m_pPageCur->OnMouseMove(wParam, LOWORD(lParam)
  158.                 , HIWORD(lParam));
  159.             break;
  160.  
  161.         case WM_NCHITTEST:
  162.             if (NULL!=ppg->m_pPageCur)
  163.                 {
  164.                 //This just saves information in the page for OnSetCursor
  165.                 ppg->m_pPageCur->OnNCHitTest(LOWORD(lParam)
  166.                     , HIWORD(lParam));
  167.                 }
  168.  
  169.             return DefWindowProc(hWnd, iMsg, wParam, lParam);
  170.  
  171.         case WM_SETCURSOR:
  172.             if (NULL!=ppg->m_pPageCur)
  173.                 {
  174.                 if (ppg->m_pPageCur->OnSetCursor(LOWORD(lParam)))
  175.                     break;
  176.                 }
  177.  
  178.             return DefWindowProc(hWnd, iMsg, wParam, lParam);
  179.  
  180.         default:
  181.             return DefWindowProc(hWnd, iMsg, wParam, lParam);
  182.         }
  183.  
  184.     ppg->m_fDirty |= fDirty;
  185.     return 0L;
  186.     }
  187.  
  188.  
  189.  
  190.  
  191.  
  192. /*
  193.  * RectConvertMappings
  194.  *
  195.  * Purpose:
  196.  *  Converts the contents of a rectangle from device to logical
  197.  *  coordinates where the hDC defines the logical coordinates.
  198.  *
  199.  * Parameters:
  200.  *  pRect           LPRECT containing the rectangle to convert.
  201.  *  hDC             HDC describing the logical coordinate system.
  202.  *                  if NULL, uses a screen DC in MM_LOMETRIC.
  203.  *  fToDevice       BOOL TRUE to convert from uConv to device,
  204.  *                  FALSE to convert device to uConv.
  205.  *
  206.  * Return Value:
  207.  *  None
  208.  */
  209.  
  210. void RectConvertMappings(LPRECT pRect, HDC hDC, BOOL fToDevice)
  211.     {
  212.     POINT   rgpt[2];
  213.     BOOL    fSysDC=FALSE;
  214.  
  215.     if (NULL==pRect)
  216.         return;
  217.  
  218.     rgpt[0].x=pRect->left;
  219.     rgpt[0].y=pRect->top;
  220.     rgpt[1].x=pRect->right;
  221.     rgpt[1].y=pRect->bottom;
  222.  
  223.     if (NULL==hDC)
  224.         {
  225.         hDC=GetDC(NULL);
  226.         SetMapMode(hDC, MM_LOMETRIC);
  227.         fSysDC=TRUE;
  228.         }
  229.  
  230.     if (fToDevice)
  231.         LPtoDP(hDC, rgpt, 2);
  232.     else
  233.         DPtoLP(hDC, rgpt, 2);
  234.  
  235.     if (fSysDC)
  236.         ReleaseDC(NULL, hDC);
  237.  
  238.     pRect->left=rgpt[0].x;
  239.     pRect->top=rgpt[0].y;
  240.     pRect->right=rgpt[1].x;
  241.     pRect->bottom=rgpt[1].y;
  242.  
  243.     return;
  244.     }
  245.  
  246.  
  247.  
  248.  
  249.  
  250.  
  251.  
  252. /*
  253.  * CPages::Draw
  254.  *
  255.  * Purpose:
  256.  *  Paints the current page in the pages window.
  257.  *
  258.  * Parameters:
  259.  *  hDC             HDC to draw on, could be a metafile or printer DC or
  260.  *                  any other type of DC.
  261.  *  fNoColor        BOOL indicating if we should use screen colors or
  262.  *                  printer colos (B&W).  Objects are printed as-is, however.
  263.  *                  This is TRUE for printer DCs or print preview.
  264.  *  fPrinter        BOOL indicating if this is a printer DC in which case
  265.  *                  we eliminate some of the fancy drawing, like shadows on
  266.  *                  the page and so forth.
  267.  *
  268.  * Return Value:
  269.  *  None
  270.  */
  271.  
  272. void CPages::Draw(HDC hDC, BOOL fNoColor, BOOL fPrinter)
  273.     {
  274.     RECT            rc, rcT;
  275.     UINT            uMM;
  276.     HPEN            hPen;
  277.     HBRUSH          hBrush;
  278.     HGDIOBJ         hObj1, hObj2;
  279.     COLORREF        cr;
  280.     char            szTemp[20];
  281.     UINT            cch;
  282.     DWORD           dwExt;
  283.     LPPAGE          pPage;
  284.     RECT            rcPos;
  285.  
  286.     //Make sure the DC is in LOMETRIC
  287.     uMM=SetMapMode(hDC, MM_LOMETRIC);
  288.  
  289.     if (!fPrinter)
  290.         {
  291.         /*
  292.          * We maintain a 6mm border around the page on the screen besides
  293.          * 12.7mm margins.  We also have to account for the scroll position
  294.          * with m_*Pos which are in pixels so we have to convert them.
  295.          */
  296.         SetRect(&rcPos, m_xPos, m_yPos, 0, 0);
  297.         RectConvertMappings(&rcPos, hDC, FALSE);
  298.  
  299.         rc.left  = LOMETRIC_BORDER-rcPos.left;